package boot;

import java.io.IOException;
import java.io.OutputStream;

import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;

import files.Files;
import function.Mp3File;

public class Pawner {

	public static void pawn(Mp3File mp3File) throws IOException,
			AlreadyPawnedException {
		String pawnedFolder = Files.BOOT_ROOT + "/Pawn";
		String title = mp3File.getTitle();
		FileConnection fc = (FileConnection) Connector.open(pawnedFolder + "/"
				+ title + ".pawn");
		if (fc.exists()) {
			throw new AlreadyPawnedException();
		}
		fc.create();
		

		OutputStream fos = fc.openOutputStream();
		fos.write(((mp3File.getFilePath()).getBytes()));
		fos.close();
	}

	public static void unpawn(Mp3File mp3File) {
		String pawnedFolder = Files.BOOT_ROOT + "/Pawn";
		String title = mp3File.getTitle();
		try {
			FileConnection fc = (FileConnection) Connector.open(pawnedFolder + "/"
					+ title + ".pawn");
			fc.delete();
		} catch (IOException e) {
			Boot.alertException(e, new Pawner(), "unpawn()");
		}
	}
}
